package airthmetic.exercise.queue;

import java.util.Stack;

/**
 * @Author douhy
 * @description 队列相关算法练习
 * @date 22/01/25
 */
public class QueueDemo {

    private Stack<Integer> stack1;
    private Stack<Integer> stack2;


    public QueueDemo() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }

    public void push(int x) {
        stack1.push(x);
    }

    public int pop() {
        if (stack2.empty()) {
            in2out();
        }
        if (stack2.empty()) return -1;
        return stack2.pop();

    }

    public int peek() {
        if (stack2.empty()) {
            in2out();
        }
        if (stack2.empty()) return -1;
        else return stack2.peek();

    }

    public boolean empty() {
        return stack1.empty() && stack2.empty();
    }

    @Override
    public String toString() {
        return "QueueDemo{" +
                "stack1=" + stack1 +
                ", stack2=" + stack2 +
                '}';
    }

    private void in2out() {
        while (!stack1.isEmpty()) {
            stack2.push(stack1.pop());
        }
    }

    public static void main(String[] args) {
        QueueDemo queueDemo = new QueueDemo();
        queueDemo.push(1);
        queueDemo.push(2);
        queueDemo.push(3);

        System.out.println(queueDemo);
        queueDemo.pop();
        System.out.println(queueDemo);
        queueDemo.pop();
        System.out.println(queueDemo);
        queueDemo.pop();
        System.out.println(queueDemo);
        queueDemo.pop();
        System.out.println(queueDemo);
    }
}

